package sysman.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;
import org.springside.core.web.StrutsAction;
import org.springside.core.web.StrutsEntityAction;

import sysman.dao.BacklogDao;
import sysman.model.Backlog;
import sysman.model.TemplateVo;
import sysman.model.User;
import util.DateUtil;
import util.LogUtil;
import util.MD5Util;

import common.Globals;


/**
 * 登陆类，用于处理用户登录，登出操作。
 * 
 */
@SuppressWarnings("unused")
public class LoginAction extends StrutsAction {

	/**
	 * 处理用户的登录操作
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().setAttribute(Globals.MAINPAGE_TEMPLATE, new TemplateVo());

		return mapping.findForward(Globals.FORWARD_MAIN);
	}

	/**
	 * 处理登出，从平台登出。
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward logoutPortal(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		@SuppressWarnings("unused")
		String subSystemTag = (String) request.getSession().getAttribute(
				Globals.SESSION_SUBSYSTEMTAG);

		return null;
	}

	/**
	 * 处理重新登录,从平台。
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward reLoginPortal(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		User userPo = (User) request.getSession().getAttribute(Globals.SESSION_USER);
		// 判断是否已经超时
		if (userPo == null) {
			return mapping.findForward(Globals.FORWARD_INDEX);
		}

		request.getSession().invalidate();
		System.out.println("已经销毁 session portal.......");

		return mapping.findForward(Globals.FORWARD_INDEX);
	}
	
	/**
	 * execute
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String method = request.getParameter("method");
		LogUtil.debug(this.getClass(), "调用方法" + method);
		
		if ("login".equals(method)) {
			try {	
				return login(mapping, form, request, response);
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("loginErr", "登陆失败,获取当前用户数据出错!");
				return mapping.findForward(Globals.FORWARD_LOGIN);
			}
		} else if("logoutPortal".equals(method)
				|| "reLoginPortal".equals(method)
				|| "forward".equals(method)
				|| "toMainContent".equals(method)
				|| "toOtherContent".equals(method)) {
			try{
				return super.execute(mapping, form, request, response);
			} catch (Exception e){				
				return mapping.findForward(Globals.FORWARD_LOGIN);
			}
		} else {
			return mapping.findForward(Globals.FORWARD_LOGIN);
		}
	}
	

}
